% Note:
% Given the target level of future asset bp, the total cost to get this
% asset is bp*( 1+phi/2*(alpha-alpha_bar)^2*sign(bp) )/( 1+rs+alpha*(r-rs)
% ). Or, put in another way, if the hhs save F and choose the domestic bond
% share as alpha, then the they have to pay adjustment cost
% phi/2*(alpha-alpha_bar)^2*|F|. 
% The effective return rate is:
% ( 1+rs+alpha(r-rs) )/( 1+phi/2*(alpha-alpha_bar)^2*sign(F) ) - 1
% If hhs are saving, they want to maximize their effective return rate; if
% they are borrowing, they want to minimize the effective return rate.
classdef PortfolioAdjCost
    properties
        AdjCost {mustBeNumeric}
        r_dom {mustBeNumeric}
        r_ext {mustBeNumeric}
        ExtBond_Target {mustBeNumeric}
    end
    
     methods
        function obj=PortfolioAdjCost(NameValueArgs)
            arguments
                NameValueArgs.AdjCost double        =   1;
                NameValueArgs.r_dom double          =   0.02/4;
                NameValueArgs.r_ext double          =   0.02/4;
                NameValueArgs.ExtBond_Target double =   0.70;
                
            end
            obj.AdjCost     =   NameValueArgs.AdjCost;
            obj.r_dom       =   NameValueArgs.r_dom;
            obj.r_ext       =   NameValueArgs.r_ext;
            obj.ExtBond_Target= NameValueArgs.ExtBond_Target;
        end
%         function EP=EffectivePrice(obj,Bp,Share_Dom)
%             EP      =   ( 1+sign(Bp).*obj.AdjCost/2.*(Share_Dom-obj.Share_Target).^2 )./ ...
%                         ( 1+obj.r_ext+Share_Dom.*(obj.r_dom-obj.r_ext) );
%         end
        function ER=EffectiveReturn(obj,Bp,Bp_ext)
            Cost    =   Bp_ext/(1+obj.r_ext) + (Bp-Bp_ext)/(1+obj.r_dom) + obj.AdjCost/2*(Bp_ext-obj.ExtBond_Target);
            ER      =   Bp/Cost -1;
        end
        function [Bp_ext,KAPPA,q_dom, q_ext]=OptChoice(obj)
            q_dom       =   1/(1+obj.r_dom);
            q_ext       =   1/(1+obj.r_ext);
            
%             B_ext = (q_dom/q_ext -1 )/obj.AdjCost + obj.ExtBond_Target;
%             KAPPA = obj.AdjCost/2 .* (B_ext - obj.ExtBond_Target).^2;
%             Bp_ext      =   (1+obj.r_ext)*B_ext;
            
            
            Bp_ext      =   (1/(1+obj.r_dom) - 1/(1+obj.r_ext))/obj.AdjCost + obj.ExtBond_Target;
            KAPPA       =   obj.AdjCost/2 .* (Bp_ext - obj.ExtBond_Target).^2;
            
        end
        
     end
end